package day07;

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class test2 {
    public static void main(String[] args) {
        
        /* 
        Random r = new Random();
        int num = 10 + r.nextInt(20 - 10 + 1);
        System.out.println(num);
        num = 10 + (int) (Math.random() * 11);
        System.out.println(num);
        */

        Random random = new Random();
        int targetNumber = random.nextInt(100) + 1;
        Scanner scanner = new Scanner(System.in);
        int guess;
        int attempts = 0;

        System.out.println("我已经想好了一个 1 - 100 的数字，你可以开始猜啦!");
        while (true) {
            try {
                guess = scanner.nextInt();
                attempts++;
                if (guess < 1 || guess > 100) {
                    System.out.println("输入的数字要在 1 - 100 之间，请重新输入。");
                } else if (guess < targetNumber) {
                    System.out.println("猜的数字小了，再试试。");
                } else if (guess > targetNumber) {
                    System.out.println("猜的数字大了，再试试。");
                } else {
                    System.out.println("恭喜你，猜对了!你一共用了 " + attempts + " 次尝试。");
                    break;
                }
            } catch (InputMismatchException e) {
                System.out.println("输入无效，请输入一个整数。");
                scanner.nextLine();
            }
        }
        scanner.close();
    }
}
